package com.xyht.sca_s.student_manage_system.modules.album.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.common.util.PinYinUtil;
import com.xyht.sca_s.student_manage_system.modules.album.entity.*;
import com.xyht.sca_s.student_manage_system.modules.album.entity.req.SmsAlbumAddReq;
import com.xyht.sca_s.student_manage_system.modules.album.entity.req.SmsAlbumPhotoDelReq;
import com.xyht.sca_s.student_manage_system.modules.album.entity.req.SmsAlbumPhotosReq;
import com.xyht.sca_s.student_manage_system.modules.album.entity.req.SmsAlbumViewAddReq;
import com.xyht.sca_s.student_manage_system.modules.album.entity.resp.*;
import com.xyht.sca_s.student_manage_system.modules.album.mapper.*;
import com.xyht.sca_s.student_manage_system.modules.album.service.SmsAlbumService;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.album.constant.AlbumAuthConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.album.constant.AlbumLikeStateConstant.ALBUM_LIKE;
import static com.xyht.sca_s.student_manage_system.modules.album.constant.AlbumLikeStateConstant.ALBUM_NOT_LIKE;
import static com.xyht.sca_s.student_manage_system.modules.album.constant.PageConstant.PAGE_NUM;
import static com.xyht.sca_s.student_manage_system.modules.album.constant.PageConstant.PAGE_SIZE;

/**
 * <p>
 * 相册主表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-07-31
 */
@Service
public class SmsAlbumServiceImpl extends ServiceImpl<SmsAlbumMapper, SmsAlbum> implements SmsAlbumService {

    @Resource
    private SmsAlbumMapper smsAlbumMapper;
    @Resource
    private SmsAlbumDetailMapper smsAlbumDetailMapper;
    @Resource
    private SmsAlbumAuthMapper smsAlbumAuthMapper;
    @Resource
    private PinYinUtil pinYinUtil;
    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private SmsAlbumViewMapper smsAlbumViewMapper;
    @Resource
    private SmsAlbumLikeMapper smsAlbumLikeMapper;
    @Resource
    private SmsRoleMapper smsRoleMapper;
    @Resource
    private SmsOrgStructureMapper smsOrgStructureMapper;

    @Override
    @Transactional
    public ResponseResult addAlbum(SmsAlbumAddReq albumAddReq, String userId) {
        if(isNullOrEmpty(albumAddReq.getAlbumName()) || isNullOrEmpty(albumAddReq.getAlbumDescribe())
                || isNullOrEmpty(albumAddReq.getAuthObjectType())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //当权限类型不为公开或者私有的时候,未指定权限人id判断为无效参数
        if(albumAddReq.getAuthObjectType() != ALBUM_AUTH_OPEN && albumAddReq.getAuthObjectType() != ALBUM_AUTH_PRIVATE){
            if(albumAddReq.getAuthObjectList() == null || albumAddReq.getAuthObjectList().size() <1){
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }
        String albumSpell = pinYinUtil.getHanZiPY(albumAddReq.getAlbumName());//名字转拼音

        //创建相册表
        SmsAlbum addAlbum = new SmsAlbum();
        BeanUtils.copyProperties(albumAddReq,addAlbum);
        addAlbum.setAlbumSpell(albumSpell);
        addAlbum.setAlbumUserId(userId);//相册创建人id
        smsAlbumMapper.insert(addAlbum);

        String albumId = addAlbum.getId();

        //创建相册权限表
        if (albumAddReq.getAuthObjectType() == ALBUM_AUTH_OPEN || albumAddReq.getAuthObjectType() == ALBUM_AUTH_PRIVATE) {
            SmsAlbumAuth addAlbumAuth = new SmsAlbumAuth();
            addAlbumAuth.setAlbumId(albumId);
            addAlbumAuth.setAuthObjectType(albumAddReq.getAuthObjectType());
            addAlbumAuth.setAlbumSpell(albumSpell);
            //如果选择私有的则存自己id
            if (albumAddReq.getAuthObjectType() == ALBUM_AUTH_PRIVATE) {
                addAlbumAuth.setAuthObjectId(userId);
            }
            smsAlbumAuthMapper.insert(addAlbumAuth);
        } else {
            for (String objectId : albumAddReq.getAuthObjectList()) {
                SmsAlbumAuth addAlbumAuth = new SmsAlbumAuth();
                addAlbumAuth.setAlbumId(albumId);
                addAlbumAuth.setAlbumSpell(albumSpell);
                addAlbumAuth.setAuthObjectType(albumAddReq.getAuthObjectType());
                addAlbumAuth.setAuthObjectId(objectId);
                smsAlbumAuthMapper.insert(addAlbumAuth);
            }
        }

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult updateAlbum(SmsAlbumAddReq albumAddReq, String userId) {
        if(isNullOrEmpty(albumAddReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAlbum album = smsAlbumMapper.selectById(albumAddReq.getId());
        if(album == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }

        if(isNullOrEmpty(albumAddReq.getAlbumName()) || isNullOrEmpty(albumAddReq.getAlbumDescribe())
                || isNullOrEmpty(albumAddReq.getAuthObjectType())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //当权限类型不为公开或者私有的时候,未指定权限人id判断为无效参数
        if (albumAddReq.getAuthObjectType() != ALBUM_AUTH_OPEN && albumAddReq.getAuthObjectType() != ALBUM_AUTH_PRIVATE) {
            if (albumAddReq.getAuthObjectList() == null || albumAddReq.getAuthObjectList().size() < 1) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }

        //修改相册表
        SmsAlbum updateAlbum = new SmsAlbum();
        BeanUtils.copyProperties(albumAddReq,updateAlbum);
        String updateAlbumName = pinYinUtil.getHanZiPY(albumAddReq.getAlbumName());
        updateAlbum.setAlbumSpell(updateAlbumName);
        smsAlbumMapper.updateById(updateAlbum);

        //修改相册权限表
        //先清空再修改
        smsAlbumAuthMapper.delete(new LambdaQueryWrapper<SmsAlbumAuth>()
        .eq(SmsAlbumAuth::getAlbumId,albumAddReq.getId()));

        if (albumAddReq.getAuthObjectType() == ALBUM_AUTH_OPEN || albumAddReq.getAuthObjectType() == ALBUM_AUTH_PRIVATE) {
            SmsAlbumAuth addAlbumAuth = new SmsAlbumAuth();
            addAlbumAuth.setAlbumId(albumAddReq.getId());
            addAlbumAuth.setAlbumSpell(updateAlbumName);
            addAlbumAuth.setAuthObjectType(albumAddReq.getAuthObjectType());
            //如果选择私有的则存自己id
            if (albumAddReq.getAuthObjectType() == ALBUM_AUTH_PRIVATE) {
                addAlbumAuth.setAuthObjectId(userId);
            }
            smsAlbumAuthMapper.insert(addAlbumAuth);
        } else {
            for (String objectId : albumAddReq.getAuthObjectList()) {
                SmsAlbumAuth addAlbumAuth = new SmsAlbumAuth();
                addAlbumAuth.setAlbumId(albumAddReq.getId());
                addAlbumAuth.setAlbumSpell(updateAlbumName);
                addAlbumAuth.setAuthObjectType(albumAddReq.getAuthObjectType());
                addAlbumAuth.setAuthObjectId(objectId);
                smsAlbumAuthMapper.insert(addAlbumAuth);
            }
        }

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deletedAlbum(String albumId, String userId) {
        if(isNullOrEmpty(albumId)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAlbum album = smsAlbumMapper.selectById(albumId);
        if(album == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }
        //删除相册表数据
        smsAlbumMapper.deleteById(albumId);
        //删除相册权限数据
        smsAlbumAuthMapper.delete(new LambdaQueryWrapper<SmsAlbumAuth>()
        .eq(SmsAlbumAuth::getAlbumId,albumId));
        //删除相册详情
        smsAlbumDetailMapper.delete(new LambdaQueryWrapper<SmsAlbumDetail>()
        .eq(SmsAlbumDetail::getAlbumId,albumId));

        return CommonResult.success();
    }

    @Override
    public ResponseResult getAlbumEdit(String albumId) {
        if (isNullOrEmpty(albumId)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAlbum smsAlbum = smsAlbumMapper.selectById(albumId);
        if (smsAlbum == null) {
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }

        SmsAlbumEditResp detailResp = new SmsAlbumEditResp();
        BeanUtils.copyProperties(smsAlbum,detailResp);

        //查询权限type
        SmsAlbumAuth smsAlbumAuth = smsAlbumAuthMapper.selectOne(new QueryWrapper<SmsAlbumAuth>()
                .select("DISTINCT auth_object_type")
                .lambda()
                .eq(SmsAlbumAuth::getAlbumId, albumId));
        detailResp.setAuthObjectType(smsAlbumAuth.getAuthObjectType());
        List<SmsAlbumUserResp> list = new ArrayList<>();
        if(smsAlbumAuth.getAuthObjectType() == ALBUM_AUTH_PERSON){
            //查询权限id集合
            List<String> objectIds = smsAlbumAuthMapper.selectList(new LambdaQueryWrapper<SmsAlbumAuth>()
                    .eq(SmsAlbumAuth::getAlbumId, albumId)).stream().map(SmsAlbumAuth::getAuthObjectId).collect(Collectors.toList());
            if(objectIds.size() > 0) {
                for (String objectId : objectIds) {
                    SmsUser userInfo = cacheUtil.getUserInfo(objectId);
                    if(userInfo != null){
                        SmsAlbumUserResp userResp = new SmsAlbumUserResp();
                        BeanUtils.copyProperties(userInfo,userResp);
                        list.add(userResp);
                    }
                }
            }
        }else if(smsAlbumAuth.getAuthObjectType() == ALBUM_AUTH_ROLE){
            //查询权限id集合
            List<String> objectIds = smsAlbumAuthMapper.selectList(new LambdaQueryWrapper<SmsAlbumAuth>()
                    .eq(SmsAlbumAuth::getAlbumId, albumId)).stream().map(SmsAlbumAuth::getAuthObjectId).collect(Collectors.toList());
            if(objectIds.size() > 0) {
                for (String objectId : objectIds) {
                    SmsRole smsRole = smsRoleMapper.selectById(objectId);
                    if(smsRole != null){
                        SmsAlbumUserResp smsAlbumUserResp = new SmsAlbumUserResp();
                        smsAlbumUserResp.setId(smsRole.getId());
                        smsAlbumUserResp.setRealName(smsRole.getRoleName());
                        list.add(smsAlbumUserResp);
                    }
                }
            }
        }else if(smsAlbumAuth.getAuthObjectType() == ALBUM_AUTH_ORG){
            //查询权限id集合
            List<String> objectIds = smsAlbumAuthMapper.selectList(new LambdaQueryWrapper<SmsAlbumAuth>()
                    .eq(SmsAlbumAuth::getAlbumId, albumId)).stream().map(SmsAlbumAuth::getAuthObjectId).collect(Collectors.toList());
            for (String objectId : objectIds) {
                SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectById(objectId);
                if(smsOrgStructure != null){
                    SmsAlbumUserResp smsAlbumUserResp = new SmsAlbumUserResp();
                    smsAlbumUserResp.setId(smsOrgStructure.getId().toString());
                    smsAlbumUserResp.setRealName(smsOrgStructure.getOrgName());
                    list.add(smsAlbumUserResp);
                }
            }
        }
        detailResp.setAlbumAuthInfo(list);
        return CommonResult.success(detailResp);
    }

    @Override
    public ResponseResult getAlbumList(String userId, Integer pageSize, Integer pageNum) {
        Page<SmsAlbumAuth> page = new Page<>(pageNum,pageSize);
        List<SmsAlbumListResp> list;
        //权限设置自己可以看自己的,公开,个人里面有他的,角色里有他的角色,组织里有他的组织
        List<String> roleList = cacheUtil.getRoleList(userId);//他的角色列表
        List<String> orgList = cacheUtil.getOrgList(userId);//他的组织列表
        LambdaQueryWrapper<SmsAlbumAuth> queryWrapper = new QueryWrapper<SmsAlbumAuth>()
                .select("DISTINCT album_id,album_spell")
                .lambda()
                .eq(SmsAlbumAuth::getAuthObjectId,userId)
                .or(smsAlbumAuthLambdaQueryWrapper -> smsAlbumAuthLambdaQueryWrapper.eq(SmsAlbumAuth::getAuthObjectType,ALBUM_AUTH_ROLE)
                        .in(SmsAlbumAuth::getAuthObjectId,roleList))
                .or(smsAlbumAuthLambdaQueryWrapper -> smsAlbumAuthLambdaQueryWrapper.eq(SmsAlbumAuth::getAuthObjectType,ALBUM_AUTH_ORG)
                        .in(SmsAlbumAuth::getAuthObjectId,orgList))
                .or(smsAlbumAuthLambdaQueryWrapper -> smsAlbumAuthLambdaQueryWrapper.eq(SmsAlbumAuth::getAuthObjectType,ALBUM_AUTH_OPEN))
                .orderByAsc(SmsAlbumAuth::getAlbumSpell);
        smsAlbumAuthMapper.selectPage(page,queryWrapper);
        list = page.getRecords()
                .stream()
                .map(smsAlbumAuth -> {
                    SmsAlbumListResp smsAlbumListResp = new SmsAlbumListResp();
                    SmsAlbum smsAlbum = smsAlbumMapper.selectById(smsAlbumAuth.getAlbumId());
                    BeanUtils.copyProperties(smsAlbum,smsAlbumListResp);
                    //相册总数
                    Integer count = smsAlbumDetailMapper.selectCount(new LambdaQueryWrapper<SmsAlbumDetail>()
                            .eq(SmsAlbumDetail::getAlbumId, smsAlbum.getId()));
                    smsAlbumListResp.setPhotoCount(count);
                    //相册浏览数
                    Integer viewCount = smsAlbumViewMapper.selectCount(new LambdaQueryWrapper<SmsAlbumView>()
                            .eq(SmsAlbumView::getViewAlbumId, smsAlbumAuth.getAlbumId()));
                    smsAlbumListResp.setAlbumViewCount(viewCount);
                    //相册点赞状态和数量
                    SmsAlbumLike smsAlbumLike = smsAlbumLikeMapper.selectOne(new LambdaQueryWrapper<SmsAlbumLike>()
                            .eq(SmsAlbumLike::getLikeAlbumId, smsAlbumAuth.getAlbumId())
                            .eq(SmsAlbumLike::getLikeAlbumUserId, userId));
                    if(smsAlbumLike != null) {
                        smsAlbumListResp.setAlbumLikeState(smsAlbumLike.getLikeState());
                    }else {
                        smsAlbumListResp.setAlbumLikeState(ALBUM_NOT_LIKE);
                    }
                    Integer likeCount = smsAlbumLikeMapper.selectCount(new LambdaQueryWrapper<SmsAlbumLike>()
                            .eq(SmsAlbumLike::getLikeAlbumId, smsAlbumAuth.getAlbumId())
                            .eq(SmsAlbumLike::getLikeState, ALBUM_LIKE));
                    smsAlbumListResp.setAlbumLikeCount(likeCount);
                    return smsAlbumListResp;
                })
                .collect(Collectors.toList());
        int total = (int)page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getAlbumId(String albumId, Integer pageSize, Integer pageNum,String userId) {
        if(isNullOrEmpty(albumId)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsAlbum smsAlbum = smsAlbumMapper.selectById(albumId);
        if(smsAlbum == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }
        //查询相册照片数据
        SmsAlbumDetailResp  smsAlbumDetailResp = new SmsAlbumDetailResp();
        List<SmsAlbumTimeAndPhotosResp> photosRespList;
        Page<SmsAlbumDetail> page = new Page<>(pageNum,pageSize);
        smsAlbumDetailMapper.selectPage(page, new QueryWrapper<SmsAlbumDetail>()
                .select("photos_time")
                .lambda()
                .eq(SmsAlbumDetail::getAlbumId, albumId)
                .groupBy(SmsAlbumDetail::getPhotosTime)
                .orderByDesc(SmsAlbumDetail::getPhotosTime));
        photosRespList = page.getRecords()
                .stream()
                .map(detail -> {
                    Integer count = smsAlbumDetailMapper.selectCount(new LambdaQueryWrapper<SmsAlbumDetail>()
                            .eq(SmsAlbumDetail::getAlbumId, albumId)
                            .eq(SmsAlbumDetail::getPhotosTime, detail.getPhotosTime()));
                    Integer leftCount =  count - PAGE_SIZE;
                    Page<SmsAlbumDetail> page1 = new Page<>(PAGE_NUM,PAGE_SIZE);
                    smsAlbumDetailMapper.selectPage(page1,new LambdaQueryWrapper<SmsAlbumDetail>()
                            .eq(SmsAlbumDetail::getAlbumId,albumId)
                            .eq(SmsAlbumDetail::getPhotosTime,detail.getPhotosTime())
                            .orderByDesc(SmsAlbumDetail::getCreateTime));
                    List<SmsAlbumPhotosResp> list = page1.getRecords()
                            .stream()
                            .map(photo ->{
                                SmsAlbumPhotosResp smsAlbumPhotosResp = new SmsAlbumPhotosResp();
                                BeanUtils.copyProperties(photo,smsAlbumPhotosResp);
                                return smsAlbumPhotosResp;
                                    }
                            ).collect(Collectors.toList());

                    SmsAlbumTimeAndPhotosResp smsAlbumTimeAndPhotosResp = new SmsAlbumTimeAndPhotosResp();
                    smsAlbumTimeAndPhotosResp.setPhotosTime(detail.getPhotosTime());
                    smsAlbumTimeAndPhotosResp.setList(list);
                    smsAlbumTimeAndPhotosResp.setLeftCount(leftCount);
                    return smsAlbumTimeAndPhotosResp;
                        }
                )
                .collect(Collectors.toList());
        smsAlbumDetailResp.setAlbumTimeAndPhotosResp(photosRespList);//照片信息
        //相册浏览数
        Integer viewCount = smsAlbumViewMapper.selectCount(new LambdaQueryWrapper<SmsAlbumView>()
                .eq(SmsAlbumView::getViewAlbumId, albumId));
        smsAlbumDetailResp.setAlbumViewCount(viewCount);
        //相册点赞状态
        SmsAlbumLike smsAlbumLike = smsAlbumLikeMapper.selectOne(new LambdaQueryWrapper<SmsAlbumLike>()
                .eq(SmsAlbumLike::getLikeAlbumId, albumId)
                .eq(SmsAlbumLike::getLikeAlbumUserId, userId));
        if(smsAlbumLike != null) {
            smsAlbumDetailResp.setAlbumLikeState(smsAlbumLike.getLikeState());
        }else {
            smsAlbumDetailResp.setAlbumLikeState(ALBUM_NOT_LIKE);
        }
        //总数
        Integer likeCount = smsAlbumLikeMapper.selectCount(new LambdaQueryWrapper<SmsAlbumLike>()
                .eq(SmsAlbumLike::getLikeAlbumId, albumId)
                .eq(SmsAlbumLike::getLikeState, ALBUM_LIKE));
        smsAlbumDetailResp.setAlbumLikeCount(likeCount);
        int total = (int)page.getTotal();
        return CommonResult.success(smsAlbumDetailResp,total);
    }

    @Override
    public ResponseResult deletedAlbumPhotos(SmsAlbumPhotoDelReq photoDelReq) {
        if(isNullOrEmpty(photoDelReq.getPhotoId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsAlbumDetail smsAlbumDetail = smsAlbumDetailMapper.selectById(photoDelReq.getPhotoId());
        if(smsAlbumDetail == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_PHOTO_NOT_EXIST);
        }

        int result = smsAlbumDetailMapper.deleteById(photoDelReq.getPhotoId());
        if(result != 1){
            return CommonResult.failed(CommonCodeEnum.DELETE_FAIL);
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult addAlbumPhotos(SmsAlbumPhotosReq addAlbumPhotosReq) {
        if(isNullOrEmpty(addAlbumPhotosReq.getAlbumId()) || isNullOrEmpty(addAlbumPhotosReq.getPhotos())
                || isNullOrEmpty(addAlbumPhotosReq.getPhotosDescribe())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsAlbum smsAlbum = smsAlbumMapper.selectById(addAlbumPhotosReq.getAlbumId());
        if(smsAlbum == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }

        if(addAlbumPhotosReq.getPhotos() != null && addAlbumPhotosReq.getPhotos().size() > 0) {
            for (String photo : addAlbumPhotosReq.getPhotos()) {
                SmsAlbumDetail smsAlbumDetail = new SmsAlbumDetail();
                BeanUtils.copyProperties(addAlbumPhotosReq, smsAlbumDetail);
                smsAlbumDetail.setPhotos(photo);
                smsAlbumDetail.setPhotosTime(new Date());
                smsAlbumDetailMapper.insert(smsAlbumDetail);
            }
        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult getAdminAlbumList(Integer pageSize, Integer pageNum,String userId) {
        Page<SmsAlbum> page = new Page<>(pageNum,pageSize);
        List<SmsAlbumListResp> albumListResp;
        smsAlbumMapper.selectPage(page,new LambdaQueryWrapper<SmsAlbum>()
        .orderByAsc(SmsAlbum::getAlbumSpell));
        albumListResp = page.getRecords()
                .stream()
                .map(album ->{
                    SmsAlbumListResp smsAlbumListResp = new SmsAlbumListResp();
                    BeanUtils.copyProperties(album,smsAlbumListResp);
                    //相册里照片总数
                    Integer count = smsAlbumDetailMapper.selectCount(new LambdaQueryWrapper<SmsAlbumDetail>()
                            .eq(SmsAlbumDetail::getAlbumId, album.getId()));
                    smsAlbumListResp.setPhotoCount(count);
                    //相册浏览数
                    Integer viewCount = smsAlbumViewMapper.selectCount(new LambdaQueryWrapper<SmsAlbumView>()
                            .eq(SmsAlbumView::getViewAlbumId, album.getId()));
                    smsAlbumListResp.setAlbumViewCount(viewCount);
                    //相册点赞状态和总数
                    SmsAlbumLike smsAlbumLike = smsAlbumLikeMapper.selectOne(new LambdaQueryWrapper<SmsAlbumLike>()
                            .eq(SmsAlbumLike::getLikeAlbumId, album.getId())
                            .eq(SmsAlbumLike::getLikeAlbumUserId, userId));
                    if(smsAlbumLike != null) {
                        smsAlbumListResp.setAlbumLikeState(smsAlbumLike.getLikeState());
                    }else {
                        smsAlbumListResp.setAlbumLikeState(ALBUM_NOT_LIKE);
                    }

                    Integer likeCount = smsAlbumLikeMapper.selectCount(new LambdaQueryWrapper<SmsAlbumLike>()
                            .eq(SmsAlbumLike::getLikeAlbumId, album.getId())
                            .eq(SmsAlbumLike::getLikeState, ALBUM_LIKE));
                    smsAlbumListResp.setAlbumLikeCount(likeCount);
                    return smsAlbumListResp;
                })
                .collect(Collectors.toList());
        int total = (int)page.getTotal();
        return CommonResult.success(albumListResp,total);
    }

    @Override
    public ResponseResult getAllPhotoByTime(String albumId, Date photoTime) {
        if(isNullOrEmpty(albumId) || isNullOrEmpty(photoTime)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAlbumPhotosResp> list;
        list = smsAlbumDetailMapper.selectList(new LambdaQueryWrapper<SmsAlbumDetail>()
                .eq(SmsAlbumDetail::getAlbumId, albumId)
                .eq(SmsAlbumDetail::getPhotosTime, photoTime)
                .orderByDesc(SmsAlbumDetail::getCreateTime))
                .stream()
                .map(photo -> {
                    SmsAlbumPhotosResp smsAlbumPhotosResp = new SmsAlbumPhotosResp();
                    BeanUtils.copyProperties(photo, smsAlbumPhotosResp);
                    return smsAlbumPhotosResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(list);
    }

    @Override
    public ResponseResult addAlbumView(SmsAlbumViewAddReq albumViewAddReq, String userId) {
        if(isNullOrEmpty(albumViewAddReq.getViewAlbumId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsAlbum smsAlbum = smsAlbumMapper.selectById(albumViewAddReq.getViewAlbumId());
        if(smsAlbum == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }
        SmsAlbumView addAlbumView = new SmsAlbumView();
        addAlbumView.setViewAlbumId(albumViewAddReq.getViewAlbumId());
        addAlbumView.setViewAlbumUserId(userId);
        smsAlbumViewMapper.insert(addAlbumView);

        return CommonResult.success();
    }

    @Override
    public ResponseResult likeAlbum(SmsAlbumViewAddReq albumViewAddReq, String userId) {
        if(isNullOrEmpty(albumViewAddReq.getViewAlbumId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsAlbum smsAlbum = smsAlbumMapper.selectById(albumViewAddReq.getViewAlbumId());
        if(smsAlbum == null){
            return CommonResult.failed(CommonCodeEnum.ALBUM_NOT_EXIST);
        }

        SmsAlbumLike smsAlbumLike = smsAlbumLikeMapper.selectOne(new LambdaQueryWrapper<SmsAlbumLike>()
                .eq(SmsAlbumLike::getLikeAlbumUserId, userId)
                .eq(SmsAlbumLike::getLikeAlbumId,albumViewAddReq.getViewAlbumId()));

        if(smsAlbumLike != null){//已有数据则为点赞,再点击就设置为取消点赞
            if(smsAlbumLike.getLikeState() == ALBUM_LIKE) {
                smsAlbumLike.setLikeState(ALBUM_NOT_LIKE);
            }else {
                smsAlbumLike.setLikeState(ALBUM_LIKE);
            }
            smsAlbumLike.setLikeAlbumId(albumViewAddReq.getViewAlbumId());
            smsAlbumLikeMapper.updateById(smsAlbumLike);
        }else {//没有数据,点击就设置为点赞状态
            SmsAlbumLike addAlbumLike = new SmsAlbumLike();
            addAlbumLike.setLikeAlbumId(albumViewAddReq.getViewAlbumId());
            addAlbumLike.setLikeAlbumUserId(userId);
            addAlbumLike.setLikeState(ALBUM_LIKE);//点赞
            smsAlbumLikeMapper.insert(addAlbumLike);
        }
        return CommonResult.success();
    }


}
